package com.itgaoshu.hospitalproject.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.hospitalproject.bean.Departments;
import com.itgaoshu.hospitalproject.service.DepartmentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("department")
public class DepartmentsController {
    @Autowired
    private DepartmentsService departmentsService;


    //查询所有科室的方法
    @RequestMapping("departmentList")
    @ResponseBody
    public Object departmentList(String department,Integer limit,Integer page){
        PageHelper.startPage(page,limit);
        Departments departments = new Departments();
        departments.setDepartment(department);
        List<Departments> departments1 = departmentsService.departmentList(departments);
        PageInfo pageInfo = new PageInfo(departments1);
        Map<String,Object> tableDate = new HashMap<>();
        tableDate.put("code",0);
        tableDate.put("msg","");
        tableDate.put("count",pageInfo.getTotal());
        tableDate.put("data",pageInfo.getList());
        return tableDate;
    }


    //添加科室的方法
    @RequestMapping("addDepartment")
    @ResponseBody
    public Object addDepartment(Departments departments){
        int count = departmentsService.count(departments);
        if(count==0){
            int result = departmentsService.addDepartment(departments);
            if (result>0){
                return "添加成功";
            }else{
                return "添加失败";
            }
        }else{
            return "该部门已经存在";
        }

    }



}




















